﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TruckingApplication.Dal.EntityModels;

namespace TruckingApplication.Web.Controllers
{ 
    public class PaymentController : Controller
    {
        private TRUCKING_FRAMEWORK_Entities_2 db = new TRUCKING_FRAMEWORK_Entities_2();

        //
        // GET: /Payment/

        public ViewResult Index()
        {
            return View(db.PAYMENTs.ToList());
        }

        //
        // GET: /Payment/Details/5

        public ViewResult Details(long id)
        {
            PAYMENT payment = db.PAYMENTs.Find(id);
            return View(payment);
        }

        //
        // GET: /Payment/Create

        public ActionResult Create(long? id)
        {
            // Get list of resrouces
            var myResources = from _r in db.RESOURCE_COMPANY
                    join rr in db.RESOURCEs on _r.resourceID equals rr.resourceID
                    join rt in db.RESOURCE_TYPE on rr.resourceTypeID equals rt.resourceTypeID
                    where rr.isActive == true
                    && _r.companyID == Dal.Infrastructure.UserSpecifics.CompanyID
                    select new { rr.FullName, rr.resourceID };

            var items = new List<SelectListItem>();

            if (id != null)
            {
                var resourceName = db.RESOURCEs.Where(r => r.resourceID == id).SingleOrDefault().FullName;

                foreach (var r in myResources)
                {
                    if (r.resourceID == id)
                        items.Add(new SelectListItem { Value = r.resourceID.ToString(), Text = r.FullName, Selected = true });
                    else
                        items.Add(new SelectListItem { Value = r.resourceID.ToString(), Text = r.FullName });
                }

                ViewBag.MyResources = new SelectList(myResources, "resourceID", "FullName", resourceName);
            }
            else
            {
                foreach (var r in myResources)
                    items.Add(new SelectListItem { Value = r.resourceID.ToString(), Text = r.FullName });
                
                ViewBag.MyResources = new SelectList(items, "Value", "Text");
            }

            return View();
        } 

        //
        // POST: /Payment/Create

        [HttpPost]
        public ActionResult Create(PAYMENT payment, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                payment.paymentType = collection["sel_paymentType"];
                if (collection["MyResources"] != "Select One" && String.IsNullOrEmpty(payment.paymentFrom))
                {
                    var rId = int.Parse(collection["MyResources"]);
                    // Assume
                    payment.paymentFrom = db.RESOURCEs.Where(d => d.resourceID == rId).SingleOrDefault().FullName;
                }
                payment.companyId = Dal.Infrastructure.UserSpecifics.CompanyID;
                db.PAYMENTs.Add(payment);
                db.SaveChanges();
                return RedirectToAction("Dashboard", "Account"); 
            }

            return View(payment);
        }
        
        //
        // GET: /Payment/Edit/5
 
        public ActionResult Edit(long id)
        {
            PAYMENT payment = db.PAYMENTs.Find(id);
            return View(payment);
        }

        //
        // POST: /Payment/Edit/5

        [HttpPost]
        public ActionResult Edit(PAYMENT payment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(payment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(payment);
        }

        //
        // GET: /Payment/Delete/5
 
        public ActionResult Delete(long id)
        {
            PAYMENT payment = db.PAYMENTs.Find(id);
            return View(payment);
        }

        //
        // POST: /Payment/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {            
            PAYMENT payment = db.PAYMENTs.Find(id);
            db.PAYMENTs.Remove(payment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult PayNow(long id)
        {
            // Find Resource
            var resource = db.RESOURCEs.Find(id);
            ViewBag.ResourceID = resource;
            return RedirectToAction("Create", "Payment", new { id = resource.resourceID });
        }
    }
}